package com.example.jl.pickpiclibrary.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;


import com.example.jl.pickpiclibrary.R;
import com.example.jl.pickpiclibrary.adapter.GridAdapter;
import com.example.jl.pickpiclibrary.utils.ImageLoader;

import java.util.ArrayList;


public class MainActivityActivity extends Activity  {
    private static final int REQUEST_PICK = 0;
    private static final int SHOW_BIG = 1;
    private GridView gridview;
    private GridAdapter adapter;
    private ArrayList<String> selectedPicture = new ArrayList<String>();
    private ArrayList<String> moreselectedPicture = new ArrayList<String>();
    private ArrayList<String> revisedPicture = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        gridview = (GridView) findViewById(R.id.gridview);
        adapter = new GridAdapter(this,selectedPicture);
        gridview.setAdapter(adapter);
    }


    public void selectPicture(View view) {
        startActivityForResult(new Intent(this, PickPicActivity.class), REQUEST_PICK);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        switch (requestCode) {
            case REQUEST_PICK:
                if (resultCode == RESULT_OK) {
                    moreselectedPicture = (ArrayList<String>) data.getSerializableExtra(PickPicActivity.INTENT_SELECTED_PICTURE);
                    selectedPicture.addAll(moreselectedPicture);
                    adapter.notifyDataSetChanged();
                }
                break;
            case SHOW_BIG:
                if (resultCode == RESULT_OK) {
                    selectedPicture.clear();
                    revisedPicture = (ArrayList<String>) data.getSerializableExtra("revisedPicture");
                    selectedPicture.addAll(revisedPicture);
                    adapter.notifyDataSetChanged();
                }
                break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            moreselectedPicture.clear();
            revisedPicture.clear();
            selectedPicture.clear();
            adapter.notifyDataSetChanged();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



}
